$(document).ready(function () {
    //初始化
    getordersList();
});
function getordersList() {
    let obj=new Object();
    obj.m="list";
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/auth/orders",
        dataType:"json",
        data:obj,
        success:function (result) {
            //console.log(result);
            if(result.code==200){
                initView(result.data);
            }
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
};


/***
 *   <tr class="bg">
 <th>订单编号</th>
 <th>金额</th>
 <th>下单日期</th>
 <th>订单状态</th>
 <th>编辑</th>
 </tr>
 *
*  <tr>
 <td>155555555555555555</td>
 <td ><span class="font_red">￥155</span></td>
 <td>2022-02-06</td>
 <td>已下单，为受理</td>
 <td><a href="#" class="selectOrder font_red">查看订单</a>
 <a href="#" class="deleteOrder">删除订单</a>
 </td>
 </tr>

 * @param result
 */
function initView(result) {
console.log("---订单页面开始渲染i----")
    //设计表头
    $("table").html("");
    let tr_header=$("<tr></tr>").addClass("bg");
    let tr_header_th1=$("<th></th>").text("订单编号");
    let tr_header_th2=$("<th></th>").text("金额");
    let tr_header_th3=$("<th></th>").text("下单日期");
    let tr_header_th4=$("<th></th>").text("订单状态");
    let tr_header_th5=$("<th></th>").text("编辑");
    tr_header.append(tr_header_th1,tr_header_th2,tr_header_th3,tr_header_th4,tr_header_th5);
    $("table").append(tr_header);
    $.each(result,function (index,element) {
        let tr=$("<tr></tr>");
        let td1=$("<td></td>").text(element.orderNO);
        let td2=$("<td></td>").html("￥ "+element.money).addClass("font_red");
        let td3=$("<td></td>").html(element.orderDate);
        let orderStatus=element.orderStatus;
        let orderText="";////1-未付款 2-已付款，未发货3-已发货，为签收4-已签收，未评价5-已评价
        if(orderStatus==1){
            orderText="未付款"
        }else if(orderStatus==2){
            orderText="已付款，未发货"
        }else if(orderStatus==3){
            orderText="已发货，未签收";
        }else if(orderStatus==4){
            orderText="已签收，未评价";
        }else if(orderStatus==5){
            orderText="已评价";
        }
        let td4=$("<td></td>").text(orderText);

        let td5=$("<td></td>");
        let td5_a_find=$("<a></a>").attr("href","#").addClass("selectOrder font_red").text("查看订单");
        let td5_a_delete=$("<a></a>").attr("href","#").addClass("deleteOrder").text("删除订单");
        td5_a_delete.bind("click",element.id,deleteOrderById);
        td5_a_find.bind("click",element.id,goOrderDetail);
        td5.append(td5_a_find,td5_a_delete);
        tr.append(td1,td2,td3,td4,td5);
        $("table").append(tr);
    }) ;
}

/**
 * 根据订单id删除订单
 * @param event
 */
function deleteOrderById(event) {
    let id=event.data;
    console.log("删除订单的id="+id);
    let obj=new Object();
    obj.m="delete";
    obj.id=id;
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/auth/orders",
        dataType:"json",
        data:obj,
        success:function (result) {
            //console.log(result);
            if(result.code==200){
                getordersList();
            }
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

/**
 * 查看订单详情
 */
function goOrderDetail(event) {
    let id=event.data;
    location.href="/html/orderDetail.html?id="+id;
}